﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using HRM.CORE;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;

    public class xucListInsurance : UserControl
    {
        private ButtonEdit btSearch;
        private IContainer components = null;
        private DataTable dt_Employee;
        private DevExpress.Utils.ImageCollection imgList;
        private ImageListBoxControl imgListEmployee;
        private string m_Code = MyLogin.Code;
        private int m_Level = MyLogin.Level;
        private SelectionMode m_ListSelectionMode = SelectionMode.One;
        private int m_Status = 0;
        private PanelControl panelControl1;

        public event ClickedEventHander Clicked;

        public xucListInsurance()
        {
            this.InitializeComponent();
        }

        private void btSearch_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            this.Search(this.dt_Employee, this.btSearch.Text);
        }

        private void btSearch_EditValueChanged(object sender, EventArgs e)
        {
            this.Search(this.dt_Employee, this.btSearch.Text);
            if (this.btSearch.Text == "")
            {
                this.btSearch.EditValue = null;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public DataTable GetDataTable()
        {
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("InsuranceCode");
            DataColumn column2 = new DataColumn("HealthInsuranceCode");
            DataColumn column3 = new DataColumn("EmployeeCode");
            DataColumn column4 = new DataColumn("FirstName");
            DataColumn column5 = new DataColumn("LastName");
            DataColumn column6 = new DataColumn("Sex");
            DataColumn column7 = new DataColumn("BirthdayDay");
            DataColumn column8 = new DataColumn("BirthdayMonth");
            DataColumn column9 = new DataColumn("BirthdayYear");
            DataColumn column10 = new DataColumn("BirthPlace");
            DataColumn column11 = new DataColumn("IDCard");
            DataColumn column12 = new DataColumn("IDCardDate");
            DataColumn column13 = new DataColumn("IDCardPlace");
            DataColumn column14 = new DataColumn("MainAddress");
            DataColumn column15 = new DataColumn("Province");
            DataColumn column16 = new DataColumn("Hospital");
            DataColumn column17 = new DataColumn("InsuranceSalary");
            DataColumn column18 = new DataColumn("Allowance1");
            DataColumn column19 = new DataColumn("Allowance2");
            DataColumn column20 = new DataColumn("Allowance3");
            DataColumn column21 = new DataColumn("Allowance4");
            DataColumn column22 = new DataColumn("Status");
            table.Columns.Add(column);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);
            table.Columns.Add(column5);
            table.Columns.Add(column6);
            table.Columns.Add(column7);
            table.Columns.Add(column8);
            table.Columns.Add(column9);
            table.Columns.Add(column10);
            table.Columns.Add(column11);
            table.Columns.Add(column12);
            table.Columns.Add(column13);
            table.Columns.Add(column14);
            table.Columns.Add(column15);
            table.Columns.Add(column16);
            table.Columns.Add(column17);
            table.Columns.Add(column18);
            table.Columns.Add(column19);
            table.Columns.Add(column20);
            table.Columns.Add(column21);
            table.Columns.Add(column22);
            for (int i = 0; i < this.imgListEmployee.SelectedItems.Count; i++)
            {
                try
                {
                    string str = (this.imgListEmployee.SelectedItems[i] as ImageListBoxItem).Value.ToString();
                    char[] separator = new char[] { '(', ')' };
                    string employeeCode = str.Split(separator)[1];
                    HRM_EMPLOYEE hrm_employee = new HRM_EMPLOYEE();
                    hrm_employee.Get(employeeCode);
                    table.Rows.Add(new object[] { 
                        hrm_employee.InsuranceCode, hrm_employee.HealthInsuranceCode, hrm_employee.EmployeeCode, hrm_employee.FirstName, hrm_employee.LastName, hrm_employee.Sex, hrm_employee.BirthdayDay, hrm_employee.BirthdayMonth, hrm_employee.BirthdayYear, hrm_employee.BirthPlace, hrm_employee.IDCard, hrm_employee.IDCardDate, hrm_employee.IDCardPlace, hrm_employee.MainAddress, hrm_employee.Province, hrm_employee.Hospital, 
                        hrm_employee.InsuranceSalary, hrm_employee.Allowance1, hrm_employee.Allowance2, hrm_employee.Allowance3, hrm_employee.Allowance4, hrm_employee.Status
                     });
                }
                catch
                {
                }
            }
            return table;
        }

        private void imgListEmployee_KeyDown(object sender, KeyEventArgs e)
        {
            this.imgListEmployee.SelectionMode = SelectionMode.MultiSimple;
            if ((e.Modifiers == Keys.Control) && (e.KeyCode == Keys.A))
            {
                this.imgListEmployee.SelectAll();
            }
        }

        private void imgListEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = (this.imgListEmployee.SelectedItem as ImageListBoxItem).Value.ToString();
                char[] separator = new char[] { '(', ')' };
                string str2 = str.Split(separator)[1];
                this.RaiseClickedEventHander(str2);
            }
            catch
            {
            }
            if (this.m_ListSelectionMode == SelectionMode.One)
            {
                if (Control.ModifierKeys != Keys.None)
                {
                    this.imgListEmployee.SelectionMode = SelectionMode.MultiSimple;
                }
                else
                {
                    this.imgListEmployee.SelectionMode = SelectionMode.One;
                }
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xucListInsurance));
            SerializableAppearanceObject appearance = new SerializableAppearanceObject();
            this.imgListEmployee = new ImageListBoxControl();
            this.imgList = new DevExpress.Utils.ImageCollection(this.components);
            this.panelControl1 = new PanelControl();
            this.btSearch = new ButtonEdit();
            ((ISupportInitialize) this.imgListEmployee).BeginInit();
            this.imgList.BeginInit();
            this.panelControl1.BeginInit();
            this.panelControl1.SuspendLayout();
            this.btSearch.Properties.BeginInit();
            base.SuspendLayout();
            this.imgListEmployee.Dock = DockStyle.Fill;
            this.imgListEmployee.HighlightedItemStyle = HighlightStyle.Skinned;
            this.imgListEmployee.HotTrackItems = true;
            this.imgListEmployee.HotTrackSelectMode = HotTrackSelectMode.SelectItemOnClick;
            this.imgListEmployee.ImageList = this.imgList;
            this.imgListEmployee.Location = new Point(0, 0x2e);
            this.imgListEmployee.Name = "imgListEmployee";
            this.imgListEmployee.Size = new Size(0x115, 430);
            this.imgListEmployee.TabIndex = 0;
            this.imgListEmployee.SelectedIndexChanged += new EventHandler(this.imgListEmployee_SelectedIndexChanged);
            this.imgListEmployee.KeyDown += new KeyEventHandler(this.imgListEmployee_KeyDown);
            this.imgList.ImageStream = (ImageCollectionStreamer) manager.GetObject("imgList.ImageStream");
            this.imgList.Images.SetKeyName(0, "male.png");
            this.imgList.Images.SetKeyName(1, "user_woman.png");
            this.panelControl1.Controls.Add(this.btSearch);
            this.panelControl1.Dock = DockStyle.Top;
            this.panelControl1.Location = new Point(0, 0);
            this.panelControl1.Name = "panelControl1";
            this.panelControl1.Size = new Size(0x115, 0x2e);
            this.panelControl1.TabIndex = 1;
            this.btSearch.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.btSearch.Location = new Point(5, 12);
            this.btSearch.Name = "btSearch";
            this.btSearch.Properties.Appearance.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.btSearch.Properties.Appearance.Options.UseFont = true;
            this.btSearch.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Glyph, "T\x00ecm", -1, true, true, false, ImageLocation.MiddleCenter, (Image) manager.GetObject("btSearch.Properties.Buttons"), new KeyShortcut(Keys.None), appearance, "", null, null, true) });
            this.btSearch.Properties.NullText = "[Lọc nh\x00e2n vi\x00ean theo từ kh\x00f3a]";
            this.btSearch.Size = new Size(0x10b, 0x16);
            this.btSearch.TabIndex = 0;
            this.btSearch.ButtonClick += new ButtonPressedEventHandler(this.btSearch_ButtonClick);
            this.btSearch.EditValueChanged += new EventHandler(this.btSearch_EditValueChanged);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.imgListEmployee);
            base.Controls.Add(this.panelControl1);
            base.Name = "xucListInsurance";
            base.Size = new Size(0x115, 0x1dc);
            ((ISupportInitialize) this.imgListEmployee).EndInit();
            this.imgList.EndInit();
            this.panelControl1.EndInit();
            this.panelControl1.ResumeLayout(false);
            this.btSearch.Properties.EndInit();
            base.ResumeLayout(false);
        }

        public void ListSelectionMode(SelectionMode mode)
        {
            this.imgListEmployee.SelectionMode = this.m_ListSelectionMode = mode;
        }

        public void LoadData(int Status)
        {
            this.m_Status = Status;
            this.dt_Employee = new HRM_INSURANCE().GetListEmployee(this.m_Level, this.m_Code, this.m_Status);
            this.imgListEmployee.Items.Clear();
            foreach (DataRow row in this.dt_Employee.Rows)
            {
                ImageListBoxItem item = new ImageListBoxItem();
                if ((bool) row["Sex"])
                {
                    item.ImageIndex = 0;
                }
                else
                {
                    item.ImageIndex = 1;
                }
                item.Value = "(" + row["EmployeeCode"].ToString() + ") - " + row["FirstName"].ToString() + " " + row["LastName"].ToString();
                this.imgListEmployee.Items.Add(item);
            }
        }

        public void RaiseClickedEventHander(string Value)
        {
            if (this.Clicked != null)
            {
                this.Clicked(this, Value);
            }
        }

        public void Search(DataTable Employees, string Value)
        {
            this.imgListEmployee.Items.Clear();
            foreach (DataRow row in Employees.Rows)
            {
                if ((row["EmployeeCode"].ToString().ToLower().Contains(Value.ToLower()) || row["FirstName"].ToString().ToLower().Contains(Value.ToLower())) || row["LastName"].ToString().ToLower().Contains(Value.ToLower()))
                {
                    ImageListBoxItem item = new ImageListBoxItem();
                    if ((bool) row["Sex"])
                    {
                        item.ImageIndex = 0;
                    }
                    else
                    {
                        item.ImageIndex = 1;
                    }
                    item.Value = "(" + row["EmployeeCode"].ToString() + ") - " + row["FirstName"].ToString() + " " + row["LastName"].ToString();
                    this.imgListEmployee.Items.Add(item);
                }
            }
        }

        public delegate void ClickedEventHander(object sender, string Value);
    }
}

